package com.smile.service;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.credentials.AssumeRoleProvider;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.security.NoSuchAlgorithmException;

public class AdminSTSSolution {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        try {
            // 使用MinIO的初始管理员账户（通常有所有权限）
            String adminAccessKey = "minioadmin";  // 默认管理员
            String adminSecretKey = "minioadmin";  // 默认密码

            AssumeRoleProvider provider = new AssumeRoleProvider(
                    "http://192.168.0.80:9000",
                    adminAccessKey,
                    adminSecretKey,
                    3600,
                    null,
                    "hz",
                    null,
                    "admin-session",
                    null,
                    null);

            io.minio.credentials.Credentials credentials = provider.fetch();
            System.out.println("使用管理员获取STS成功");
            System.out.println(credentials.accessKey());

            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://192.168.0.80:9000")
                    .credentialsProvider(provider)
                    .build();
            String filename = "credentials.json";
            //对象流，获取文件
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\DELL\\Downloads\\" + "credentials.json"));
            //使用mc对象上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    //桶名和aws服务端点
                    .bucket("my-bucket").region("hz")
                    //前缀和对象名
                    .object("wayline/" + filename)
                    .stream(bis, bis.available(), -1)
                    .build());

        } catch (Exception e) {
            System.out.println("管理员也失败: " + e.getMessage());
            e.printStackTrace();
        }


    }
}